package com.zhouxin.customview.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import com.zhouxin.customview.extensions.dp2px

/**
 * Copyright©
 * FileName: CircleView
 * Author: ZhouXin
 * Date: 2022/6/20
 * Description: 圆形View
 */
private val RADIUS = 100.dp2px
private val PADDING = 50.dp2px

class CircleView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val size = ((RADIUS + PADDING) * 2).toInt()
        /* val specWidthMode = MeasureSpec.getMode(widthMeasureSpec)
         val specWidthSize = MeasureSpec.getSize(widthMeasureSpec)
         val width = when(specWidthMode) {
             MeasureSpec.EXACTLY -> specWidthSize
             MeasureSpec.AT_MOST -> if(size > specWidthSize) specWidthSize else size
             else -> size
         }*/
        //resolveSize()就是上面的写法
        val width = resolveSize(size, widthMeasureSpec)
        val height = resolveSize(size, heightMeasureSpec)
        setMeasuredDimension(width, height)
    }

    override fun onDraw(canvas: Canvas) {
        canvas.drawCircle(RADIUS + PADDING, RADIUS + PADDING, RADIUS, paint)
    }
}